﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Web.BBS;
using PowerCreator.LMS.BBS.Entity;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_BBSManager_ManagerModule : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerBBS objManagerBBS = new ManagerBBS();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();

            BinderRpt();
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lab_ManagerModule_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    //绑定Rpt
    private void BinderRpt()
    {
        string search = "";
        DataTable dt = objManagerBBS.QueryDomain(search);
        ArrayList list = new ArrayList();
        list = GetDepartmentTree(dt);
        if (list.Count == 0)
        {
            ListItem li = new ListItem();
            li.Text = GetLanageValueByKey("lab_ManagerModule_DefaultType", PowerCreator.LMS.Language.LanagetItemType.Page);
            li.Value = "0";
            list.Add(li);
        }

        rptDepartment.DataSource = list;
        rptDepartment.DataBind();

        for (int i = 0; i < rptDepartment.Items.Count; i++)
        {
            Button btn_Add = (Button)rptDepartment.Items[i].FindControl("btn_Add");
            Button btn_Editor = (Button)rptDepartment.Items[i].FindControl("btn_Editor");
            Button Button1 = (Button)rptDepartment.Items[i].FindControl("Button1");

            btn_Add.Text = GetLanageValueByKey("btn_AddCourseInfo_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
            btn_Editor.Text = GetLanageValueByKey("btn_ManagerQuestion_Edit", PowerCreator.LMS.Language.LanagetItemType.Page);
            Button1.Text = GetLanageValueByKey("btn_ManagerQuestion_Delete", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
    }

    protected void rptDepartment_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "btnadd")
        {
            string url = "AddBBSModule.aspx?DomainID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btnedit")
        {
            string url = "EditBBSModule.aspx?DomainID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "btndel")
        {
            int ID = int.Parse(e.CommandArgument.ToString());

            int count = objManagerBBS.CountDomain(" DomainFatherID=" + ID.ToString());
            if (count > 0)
            {

                Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
                lblmsg.Text = GetLanageValueByKey("msg_ManagerModule_msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
                return;
            }
            int ThemeCount=objManagerBBS.CountTheme(" ThemeDomainID=" + ID.ToString());
            if (ThemeCount>0)
            {
               Label lblmsg = (Label)e.Item.FindControl("lbl_msg_description");
               lblmsg.Text = GetLanageValueByKey("msg_ManagerModule_msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
                return;
            }

            if (objManagerBBS.DeleteDomain(int.Parse(e.CommandArgument.ToString())))
            {

                BinderRpt();
            }
        }
    }

    /// <summary>
    /// 获取树结构类型的部门信息，List中为Text和Value值，ListItem项
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public ArrayList GetDepartmentTree(DataTable dt)
    {
        ArrayList list = new ArrayList();
        InitTree(null, dt, "0", ref list);
        return list;
    }

    /// <summary>
    /// 递归初始化树结构，在ArrayList方式显示
    /// </summary>
    /// <param name="pNode">TreeNode节点</param>
    /// <param name="dt">部门数据</param>
    /// <param name="parentid">上一级部门ID</param>
    /// <param name="listt"></param>
    private void InitTree(TreeNode pNode, DataTable dt, string parentid, ref ArrayList listt)
    {
        if (dt.Rows.Count > 0)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[DomainFatherID]=" + parentid;
            foreach (DataRowView drv in dv)
            {
                TreeNode tempNode = new TreeNode();
                if (pNode == null)
                {
                    //填充根节点
                    tempNode.Text = drv["DomainName"].ToString() + "&nbsp;";
                    tempNode.ShowCheckBox = true;
                    tempNode.Value = drv["DomainID"].ToString();

                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["DomainName"].ToString();
                    li.Value = drv["DomainID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["DomainID"].ToString(), ref listt);
                }
                else
                {
                    tempNode.Text = drv["DomainName"].ToString();
                    tempNode.Value = drv["DomainID"].ToString();
                    pNode.ChildNodes.Add(tempNode);
                    int layer = tempNode.Depth;
                    string span = "";
                    if (layer != 0)
                    {
                        for (int j = 0; j < layer; j++)
                        {
                            span += "　";
                        }
                        span += "|-";
                    }

                    ListItem li = new ListItem();
                    li.Text = span + drv["DomainName"].ToString();
                    li.Value = drv["DomainID"].ToString();

                    listt.Add(li);

                    InitTree(tempNode, dt, drv["DomainID"].ToString(), ref listt);
                }
            }
        }
    }
    protected void rptDepartment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        
        
    }
}
